<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <div class="component-style">
      <div class="header">
        <p class="title">内容模板</p>
      </div>
      <el-radio-group v-model="showComponentData.styleLayout">
        <el-radio :value="1">模板一</el-radio>
        <el-radio :value="2">模板二</el-radio>
        <el-radio :value="3">模板三</el-radio>
      </el-radio-group>
    </div>

    <template v-if="showComponentData.styleLayout === 1">
      <div class="component-style">
        <div class="header" @click="contentCollapsed = !contentCollapsed">
          <p class="title">内容样式</p>
          <el-icon v-if="!contentCollapsed"><ArrowDown /></el-icon>
          <el-icon v-else><ArrowRight /></el-icon>
        </div>

        <div v-if="!contentCollapsed">
          <el-form :model="form" label-width="auto">
            <el-form-item label="圆圈一背景色：">
              <el-color-picker v-model="form.practiceBackGroundColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.practiceBackGroundColor"
              >
                <template #append>
                  <el-button @click="form.practiceBackGroundColor = '#1DC108'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="圆圈一文案：">
              <el-input
                v-model="showComponentData.practiceCircle.title"
                maxlength="4"
                show-word-limit
              ></el-input>
            </el-form-item>

            <el-form-item label="圆圈二背景色：">
              <el-color-picker v-model="form.examBackGroundColor"></el-color-picker>
              <el-input style="width: 160px; margin-left: 10px" v-model="form.examBackGroundColor">
                <template #append>
                  <el-button @click="form.examBackGroundColor = '#295CF5'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="圆圈二文案：">
              <el-input
                v-model="showComponentData.examCircle.title"
                maxlength="4"
                show-word-limit
              ></el-input>
            </el-form-item>

            <el-form-item label="按钮形状：">
              <el-radio-group v-model="form.borderRadius">
                <el-radio label="正常" :value="0" />
                <el-radio label="圆角" :value="12" />
                <el-radio label="圆形" :value="100" />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="按钮大小：">
              <div class="flex">
                <el-slider style="width: 70%" v-model="form.width" :min="10" :max="60" />
                <el-input
                  type="number"
                  style="width: 30%; margin-left: 20px"
                  v-model.number="form.width"
                  :min="10"
                  :max="60"
                >
                  <template #append>px</template>
                </el-input>
              </div>
            </el-form-item>

            <!--          <el-form-item label="文字显示：">-->
            <!--            <el-radio-group v-model="showComponentData.show_item_name">-->
            <!--              <el-radio label="显示" :value="true" />-->
            <!--              <el-radio label="隐藏" :value="false" />-->
            <!--            </el-radio-group>-->
            <!--          </el-form-item>-->

            <el-form-item label="文字大小：">
              <div class="flex">
                <el-slider style="width: 70%" v-model="form.fontSize" :min="12" :max="18" />
                <el-input
                  type="number"
                  style="width: 30%; margin-left: 20px"
                  v-model.number="form.fontSize"
                  :min="12"
                  :max="18"
                >
                  <template #append>px</template>
                </el-input>
              </div>
            </el-form-item>

            <el-form-item label="文字颜色：">
              <el-color-picker v-model="form.color"></el-color-picker>
              <el-input style="width: 160px; margin-left: 10px" v-model="form.color">
                <template #append>
                  <el-button @click="form.color = '#333333'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="文字粗细：">
              <el-radio-group v-model="form.fontWeight">
                <el-radio label="正常" :value="0" />
                <el-radio label="加粗" :value="600" />
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <div class="add-area">
        <p class="add-title">添加题型</p>
        <VueDraggable
          v-model="showComponentData.question_list"
          :animation="150"
          ghostClass="ghost"
          class="content"
          :filter="'.el-input'"
          :preventOnFilter="false"
          @end="handleUpdateComponent"
        >
          <div
            v-for="(item, index) in showComponentData.question_list"
            :key="item.id"
            class="carousel-item"
          >
            <div class="upload">
              <div class="avatar-uploader" @click="handleChangeImage(index)">
                <el-image v-if="item.img" :src="item.img" class="avatar" fit="fill"></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"
                  ><el-icon> <Plus /> </el-icon
                ></i>
                <div v-if="item.img" class="mask">替换</div>
                <el-icon
                  v-if="item.img"
                  class="del-image"
                  :size="20"
                  @click.stop="handleDelImg(index)"
                  ><CircleCloseFilled
                /></el-icon>
              </div>
            </div>
            <div class="operation">
              <div class="column">
                <p class="title">题型名称</p>
                <el-input v-model="item.name" maxlength="6" show-word-limit></el-input>
              </div>
              <div class="column">
                <p class="title">跳转链接</p>
                <div class="link">
                  <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                  <el-link type="primary" @click="handleOpenLinkDialog(index)">{{
                    item.link?.name || '请选择跳转链接'
                  }}</el-link></div
                >
              </div>
            </div>

            <el-icon class="del-icon" :size="16" color="red" @click="handleDel(index)"
              ><CircleCloseFilled
            /></el-icon>
          </div>

          <div>
            <!--          <el-button type="primary" @click="handleAdd" plain :icon="Plus" style="width: 100%"-->
            <!--            >添加题库</el-button-->
            <!--          >-->

            <el-tag type="primary" class="submit-btn" @click="handleAdd"
              >添加题库({{ showComponentData.question_list.length }}/8)</el-tag
            >
          </div>
        </VueDraggable>
      </div>
    </template>

    <template v-if="showComponentData.styleLayout === 2">
      <div class="component-style">
        <div class="header" @click="contentCollapsed = !contentCollapsed">
          <p class="title">内容样式</p>
          <el-icon v-if="!contentCollapsed"><ArrowDown /></el-icon>
          <el-icon v-else><ArrowRight /></el-icon>
        </div>

        <div v-if="!contentCollapsed">
          <el-form :model="form" label-width="auto">
            <el-form-item label="左侧背景图：">
              <div class="upload-other">
                <div class="avatar-uploader" @click="chooseLeftImage('leftBg')">
                  <el-image
                    v-if="form.styleLayout2.leftBg"
                    :src="form.styleLayout2.leftBg"
                    class="avatar"
                    fit="fill"
                  ></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"
                    ><el-icon> <Plus /> </el-icon
                  ></i>
                  <div v-if="form.styleLayout2.leftBg" class="mask">替换</div>
                  <el-icon
                    v-if="form.styleLayout2.leftBg"
                    class="del-image"
                    :size="20"
                    @click.stop="delChooseImage('leftBg')"
                    ><CircleCloseFilled
                  /></el-icon>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="左侧文字颜色：">
              <el-color-picker v-model="form.styleLayout2.leftFontColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout2.leftFontColor"
              >
                <template #append>
                  <el-button @click="form.styleLayout2.leftFontColor = '#333333'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="右上背景图：">
              <div class="upload-other">
                <div class="avatar-uploader" @click="chooseLeftImage('rightOneBg')">
                  <el-image
                    v-if="form.styleLayout2.rightOneBg"
                    :src="form.styleLayout2.rightOneBg"
                    class="avatar"
                    fit="fill"
                  ></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"
                    ><el-icon> <Plus /> </el-icon
                  ></i>
                  <div v-if="form.styleLayout2.rightOneBg" class="mask">替换</div>
                  <el-icon
                    v-if="form.styleLayout2.rightOneBg"
                    class="del-image"
                    :size="20"
                    @click.stop="delChooseImage('rightOneBg')"
                    ><CircleCloseFilled
                  /></el-icon>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="右上文字颜色：">
              <el-color-picker v-model="form.styleLayout2.rightOneFontColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout2.rightOneFontColor"
              >
                <template #append>
                  <el-button @click="form.styleLayout2.rightOneFontColor = '#333333'"
                    >重置</el-button
                  >
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="右下背景图：">
              <div class="upload-other">
                <div class="avatar-uploader" @click="chooseLeftImage('rightTwoBg')">
                  <el-image
                    v-if="form.styleLayout2.rightTwoBg"
                    :src="form.styleLayout2.rightTwoBg"
                    class="avatar"
                    fit="fill"
                  ></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"
                    ><el-icon> <Plus /> </el-icon
                  ></i>
                  <div v-if="form.styleLayout2.rightTwoBg" class="mask">替换</div>
                  <el-icon
                    v-if="form.styleLayout2.rightTwoBg"
                    class="del-image"
                    :size="20"
                    @click.stop="delChooseImage('rightTwoBg')"
                    ><CircleCloseFilled
                  /></el-icon>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="右上文字颜色：">
              <el-color-picker v-model="form.styleLayout2.rightTwoFontColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout2.rightTwoFontColor"
              >
                <template #append>
                  <el-button @click="form.styleLayout2.rightTwoFontColor = '#333333'"
                    >重置</el-button
                  >
                </template>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </template>

    <template v-if="showComponentData.styleLayout === 3">
      <div class="component-style">
        <div class="header" @click="contentCollapsed = !contentCollapsed">
          <p class="title">内容样式</p>
          <el-icon v-if="!contentCollapsed"><ArrowDown /></el-icon>
          <el-icon v-else><ArrowRight /></el-icon>
        </div>

        <div v-if="!contentCollapsed">
          <el-form :model="form" label-width="auto">
            <el-form-item label="左侧按钮文字颜色：">
              <el-color-picker v-model="form.styleLayout3.leftBtnFontColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout3.leftBtnFontColor"
              >
                <template #append>
                  <el-button @click="form.styleLayout3.leftBtnFontColor = '#fff'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="左侧按钮颜色：">
              <el-color-picker v-model="form.styleLayout3.leftBtnBg"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout3.leftBtnBg"
              >
                <template #append>
                  <el-button @click="form.styleLayout3.leftBtnBg = '#887cfa'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="右侧按钮文字颜色：">
              <el-color-picker v-model="form.styleLayout3.rightBtnFontColor"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout3.rightBtnFontColor"
              >
                <template #append>
                  <el-button @click="form.styleLayout3.rightBtnFontColor = '#fff'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="右侧按钮颜色：">
              <el-color-picker v-model="form.styleLayout3.rightBtnBg"></el-color-picker>
              <el-input
                style="width: 160px; margin-left: 10px"
                v-model="form.styleLayout3.rightBtnBg"
              >
                <template #append>
                  <el-button @click="form.styleLayout3.rightBtnBg = '#8ca5ff'">重置</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </template>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      link-type="exam"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="true"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { Plus, CircleCloseFilled, ArrowRight, ArrowDown, Link } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import { reactive, toRaw } from 'vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const contentCollapsed = ref(false)
  const showImageChooseDialog = ref(false)
  const showLinkDataDialog = ref(false)
  const changeIndex = ref()
  const chooseUploadKey = ref('')

  interface FormData {
    fontSize: number
    color: string
    fontWeight: number
    borderRadius: number
    practiceBackGroundColor: string
    examBackGroundColor: string
    width: number
    styleLayout2?: any
    styleLayout3?: any
  }

  const form = reactive<FormData>({
    fontSize: 14,
    color: '#333333',
    fontWeight: 0,
    borderRadius: 100,
    practiceBackGroundColor: '#1DC108',
    examBackGroundColor: '#295CF5',
    width: 40,
    styleLayout2: {
      leftBg: 'https://image.lutongjiakao.com/system/29d976bdf41d47baa4b4d42046c4ed1a.png',
      leftFontColor: '#fff',
      rightOneBg: 'https://image.lutongjiakao.com/system/0d22d190685f4782b6dbdb213b94c86b.png',
      rightOneFontColor: '#fff',
      rightTwoBg: 'https://image.lutongjiakao.com/system/a6c860e47d7344e0a3804d64cbf12216.png',
      rightTwoFontColor: '#fff'
    },
    styleLayout3: {
      leftBtnFontColor: '#fff',
      leftBtnBg: '#887cfa',
      rightBtnFontColor: '#fff',
      rightBtnBg: '#8ca5ff'
    }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.practiceCircle.bgColor = raw.practiceBackGroundColor
    showComponentData.value.examCircle.bgColor = raw.examBackGroundColor
    showComponentData.value.itemStyle = raw
    handleUpdateComponent()
  }, 300)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleAdd() {
    if (showComponentData.value.question_list.length < 8) {
      showComponentData.value.question_list.push({
        name: '标题',
        img: 'https://image.lutongjiakao.com/system/ed20cdb81fe141bc9511bc27eab8927a.png'
      })
    }
  }

  function handleOpenLinkDialog(index: number) {
    showLinkDataDialog.value = true
    changeIndex.value = index
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.question_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleDelImg(index: number) {
    showComponentData.value.question_list[index].img = ''
    handleUpdateComponent()
  }

  function chooseLeftImage(key: string) {
    chooseUploadKey.value = key
    showImageChooseDialog.value = true
  }

  function delChooseImage(key: string) {
    form.styleLayout2[key] = ''
  }

  function handleChangeImage(index: number) {
    changeIndex.value = index
    showImageChooseDialog.value = true
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.question_list.push(...value)
      } else {
        if (chooseUploadKey.value) {
          form.styleLayout2[chooseUploadKey.value] = value.fileUrl
        } else {
          showComponentData.value.question_list[changeIndex.value].img = value.fileUrl
        }
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  function handleDel(index: number) {
    showComponentData.value.question_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  onMounted(() => {
    if (showComponentData.value.practiceCircle.bgColor) {
      form.practiceBackGroundColor = showComponentData.value.practiceCircle.bgColor
    }
    if (showComponentData.value.examCircle.bgColor) {
      form.examBackGroundColor = showComponentData.value.examCircle.bgColor
    }

    if (showComponentData.value.itemStyle.fontSize) {
      form.fontSize = showComponentData.value.itemStyle.fontSize
      form.fontWeight = showComponentData.value.itemStyle.fontWeight
      form.borderRadius = showComponentData.value.itemStyle.borderRadius
      form.color = showComponentData.value.itemStyle.color
    }

    if (showComponentData.value.itemStyle.styleLayout2) {
      form.styleLayout2 = showComponentData.value.itemStyle.styleLayout2
    }

    if (showComponentData.value.itemStyle.styleLayout3) {
      form.styleLayout3 = showComponentData.value.itemStyle.styleLayout3
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;
      margin-top: 20px;

      .add-title {
        font-size: 14px;
        font-weight: bold;
        margin-bottom: 20px;
        color: black;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          display: flex;
          align-items: center;
          position: relative;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .avatar-uploader {
            width: 70px;
            height: 70px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            margin-right: 30px;
            text-align: center;

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 24px;
              background-color: rgba(0, 0, 0, 0.6);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;
              color: #fff;
              font-size: 12px;
            }

            .del-image {
              display: none;
              position: absolute;
              top: 0;
              right: 0;
              cursor: pointer;
              color: #8c939d;
            }

            &:hover .mask {
              opacity: 1;
            }

            &:hover .del-image {
              display: block;
            }
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 70px;
            height: 70px;
            line-height: 70px;
            text-align: center;
          }

          .avatar {
            width: 100%;
            height: 70px;
            display: block;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }

          .operation {
            flex: 1;

            .column {
              display: flex;
              align-items: center;
              margin-bottom: 6px;
              width: 100%;

              .title {
                width: 80px;
                font-size: 14px;
                margin-right: 20px;
              }

              .link {
                width: 100%;
                display: flex;
                align-items: center;
                height: 40px;
                border-radius: 6px;
                background-color: #fff;
                padding-left: 10px;
                font-size: 14px;
                color: var(--el-color-primary);
              }
            }
          }
        }
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }

    .component-style {
      width: 100%;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .title {
          font-size: 14px;
          font-weight: bold;
          color: black;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }
    .upload-other {
      .avatar-uploader {
        width: 70px;
        height: 70px;
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        margin-right: 30px;
        text-align: center;

        .mask {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 24px;
          background-color: rgba(0, 0, 0, 0.6);
          display: flex;
          align-items: center;
          justify-content: center;
          opacity: 0;
          transition: opacity 0.3s ease;
          color: #fff;
          font-size: 12px;
        }

        .del-image {
          display: none;
          position: absolute;
          top: 0;
          right: 0;
          cursor: pointer;
          color: #8c939d;
        }

        &:hover .mask {
          opacity: 1;
        }

        &:hover .del-image {
          display: block;
        }
      }

      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 70px;
        height: 70px;
        line-height: 70px;
        text-align: center;
      }

      .avatar {
        width: 100%;
        height: 70px;
        display: block;
      }

      .del-icon {
        display: none;
        position: absolute;
        top: -2px;
        right: -2px;
        cursor: pointer;
        color: #8c939d;
      }
    }
  }
</style>
